/*
 * Gui.java
 *
 * Created on 6 de septiembre de 2008, 13:39
 */

package simulator;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.plaf.FileChooserUI;
import javax.swing.table.TableColumn;

/**
 *
 * @author  Administrador
 */
public class Gui extends javax.swing.JFrame {
    OperatingSystem os;
    /** Creates new form Gui */
    public Gui() {
        try{
            os = OperatingSystem.getInstance();
        }catch(OutOfProcessesException e){
            System.err.println("Fatal error, no process in scheduler");
        }
        initComponents();
        
    }
    private void disableRunElements(){
        jMenuItem5.setEnabled(false);
        jMenuItem6.setEnabled(false);        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        memoryFrame = new javax.swing.JFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        memoryArea = new javax.swing.JTextArea();
        schedulerFrame = new javax.swing.JFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        schedulerTextArea = new javax.swing.JTextArea();
        TextPane = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        memoryArea.setColumns(20);
        memoryArea.setRows(5);
        jScrollPane1.setViewportView(memoryArea);

        javax.swing.GroupLayout memoryFrameLayout = new javax.swing.GroupLayout(memoryFrame.getContentPane());
        memoryFrame.getContentPane().setLayout(memoryFrameLayout);
        memoryFrameLayout.setHorizontalGroup(
            memoryFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        memoryFrameLayout.setVerticalGroup(
            memoryFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        schedulerTextArea.setColumns(20);
        schedulerTextArea.setRows(5);
        jScrollPane2.setViewportView(schedulerTextArea);

        javax.swing.GroupLayout schedulerFrameLayout = new javax.swing.GroupLayout(schedulerFrame.getContentPane());
        schedulerFrame.getContentPane().setLayout(schedulerFrameLayout);
        schedulerFrameLayout.setHorizontalGroup(
            schedulerFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        schedulerFrameLayout.setVerticalGroup(
            schedulerFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        TextPane.setViewportView(jTextArea1);

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Load");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Excecute");

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Advance");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Interrupt");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setText("Reset");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Memory");

        jMenuItem8.setText("Show Data Memory");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem8);

        jMenuItem9.setText("Show Instruction Memory");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem9);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Scheduler");

        jMenuItem2.setText("View Scheduler");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem2);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TextPane, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TextPane, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(this);
        File lotFile = fc.getSelectedFile();
        os.loadLotFile(lotFile.getAbsolutePath());
        try{
            os.initProcessor();
        } catch (StackUnderFlowException ex) {
            System.err.println("Stack underflow ???");
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        schedulerTextArea.setText("");
        schedulerTextArea.setText(os.getSchedulerState());        
        schedulerFrame.pack();
        schedulerFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
      
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
       
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        try{
            os.advanceStep();
            jTextArea1.append(os.getProcessorState());
        }catch(ProgrammEndedException e){
            disableRunElements();            
        }catch(AddressOverFlowException e){
            System.err.println("Address Overflow error");
        }catch(PcOverFlowException e){
            System.err.println("Pc Overflow error");
        }catch(StackOverflowException e){
            System.err.println("StackOverflowException");
        }catch(StackUnderFlowException e){
            System.err.println("Stack Underflow Exception");
        }catch(OpperationNotExistsException e){
            System.err.println("Wrong operation code error");
        }catch(UnsuportedItException e){
            System.err.println("Unsuported interruption");
        }catch(OutOfProcessesException e){
            System.err.println("Scheduler out of processes");
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        String itNr = JOptionPane.showInputDialog("interupt nr 0-7 ?");
        try{
            os.interrupt(Integer.parseInt(itNr));
        }catch(UnsuportedItException e){            
            JOptionPane.showMessageDialog(this, "Esa Interrupción no existe");
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(this, "Ingrese un numero");
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        memoryArea.setText("");
        int dataAddress =0;
        for(int i=0;i<256;i++){
            for(int j=0;j<16;j++){
                memoryArea.append(Ram.getInstance().getData(dataAddress)+"  ");
                dataAddress++;
            }
            memoryArea.append("\n");
        }
        memoryFrame.pack();
        memoryFrame.setVisible(true);
        
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        memoryArea.setText("");
        int instructionAddress =0;
        for(int i=0;i<256;i++){
            for(int j=0;j<16;j++){
                memoryArea.append(Ram.getInstance().getInstruction(instructionAddress)+"  ");
                instructionAddress++;
            }
            memoryArea.append("\n");
        }
        memoryFrame.pack();
        memoryFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem9ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane TextPane;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea memoryArea;
    private javax.swing.JFrame memoryFrame;
    private javax.swing.JFrame schedulerFrame;
    private javax.swing.JTextArea schedulerTextArea;
    // End of variables declaration//GEN-END:variables
    
}
